<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

$root_path = "../";

if (isset($_POST["submit_mail"])){

    $main_site_title = $_POST["site_title"];
    $site = $_POST["site"];

    include_once("../includes/utils.php");
    include_once("../includes/anticaptcha.php");
    include_once("../sites/$site/config.php");



    // first check the recaptcha answer

    //if ( $_POST["recaptcha_validation"] != '1' ){
        //die("<font color='#9E0000'>"._("The reCAPTCHA was entered <b>incorrectly</b>. Please try again."));
    //}

    $name = $_POST["name"];
    $mail = $_POST["email"];
    $phone = $_POST["phone"];
    $message = "
Name: $name
Email: $mail
Phone: $phone
Message:
${_POST['message']}
";
    $key = "MAILFORM%€312!fsg!$%*7";
    $dest = string_decrypt($_POST["dest"], $key);

    // validate fields
    $valid = 1;
    if (strrpos($mail, "@") == 0) {
        $valid = 0;
        echo "<font color='#9E0000'>"._("Please enter a valid email address").".</font>";
    }
    //print_r($_POST);
    if (!ac_check_answer($_POST["security_answer"], $_POST["ac_answer"])){
        $valid = 0;
        echo "<font color='#9E0000'>"._("The security question was answered incorrectly, please try again").".</font>";
    }

    $headers = "From: $name <$mail>" . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    if ($valid){

            if (mail($dest, _("Message from $main_site_title"), $message, $headers)){;
                echo "success";
            } else {
                echo "<font color='#9E0000'>"._("Mail could not be sent, if this error persist you can contact us through $dest").".</font>";

            }
        }




} else {

    function execute_mailform($site_content){

        $html = str_get_html($site_content);

        foreach($html->find("a") as $element){
            if ($element->class == "ekkin"){
                if ($element->id == "mail"){
                    $mail = $element->innertext;
                    $element -> outertext = generate_mailform($mail);
                }

            }
        }


        return $html->save();
    }
}



function generate_mailform($mail){
    include_once("includes/anticaptcha.php");
    global $lang, $main_site_title, $site;
    $key = "MAILFORM%€312!fsg!$%*7";

    $mail = string_encrypt($mail, $key);

    $form = "<script type='text/javascript' src='${relpath}ext_lib/webtoolkit.aim.js'></script>
    <script type='text/javascript' src='${relpath}ext_lib/recaptcha.js'></script>
    <script language=\"javascript\">

function startCallback() {
    // make something useful before submit (onStart)
    // check recaptcha code

    //var result = recaptchaGetValidation();
    //document.getElementById('response').innerHTML = result

    //if ( result == true ) {
    //var form = document.forms['form_mail'];
    //    form.elements['recaptcha_validation'].value = '1';
    //}
    //else {
    //var form = document.forms['form_mail'];
    //    form.elements['recaptcha_validation'].value = '0';
    //}

    return true;



}

function completeCallback(response) {
    // make something useful after (onComplete)
    // document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1;

    if (response == \"success\"){
        document.getElementById('form').innerHTML = \"<b>"._("Message sent succesfully. We will get back at you as soon as possible.")."</b>\";
        document.getElementById('response').innerHTML = \"\";
    } else {
        document.getElementById('response').innerHTML = response;
    }

}
</script>
<div id=\"form\">
<form method='POST' name='form_mail' action='plugins/mailform.php' onsubmit='return AIM.submit(this, {onStart : startCallback, onComplete : completeCallback})'>
<table>
    <tr>
        <td> "._("Name").": </td>
        <td> <input type='text' name='name'/></td>
    </tr>
    <tr>
        <td> "._("E-Mail").": </td>
        <td> <input type='text' name='email'/></td>
    </tr>
    <tr>
        <td> "._("Telephone (optional)").": </td>
        <td> <input type='text' name='phone'/></td>
    </tr>
    <tr>
        <td colspan=2>
            "._("Message").":
        </td>
    </tr>
    <tr>
        <td colspan=2>
            <textarea rows='7' cols='40' name='message'></textarea>

        </td>
    </tr>
    <tr>
        <td colspan=2><p>$anticaptcha</p></td>
    </tr>
</table>
<input type='hidden' name='dest' value='$mail'/>

<!--
<input type='hidden' name='recaptcha_validation' value='0'/>
<br/>
"._("Please enter the two words in the box below").":<br/>
<div id=\"captchadiv\"></div>
<script type=\"text/javascript\" src=\"http://www.google.com/recaptcha/api/js/recaptcha_ajax.js\"></script>
<script type=\"text/javascript\">
    function showRecaptcha(element) {
        Recaptcha.create(\"6LeDYsASAAAAAOS-XKwyCrEik7ny-eV2aRCgGmM0\", element, {
        theme: \"white\",
        callback: Recaptcha.focus_response_field});
    }
    showRecaptcha('captchadiv');
</script-->
<br/>

<input type='hidden' name='site' value='$site'/>
<input type='hidden' name='site_title' value='$main_site_title'/>

<input type='submit' name='submit_mail' value='"._("Send")."'/>
</div>
<br/>
</form><div id='response'></div>";
    return $form;
}





// your form must include form_dest, form_mail and optionally form_return and form_subject


/*
$from_mail = $_POST["form_dest"];
$mail = $_POST["form_mail"];
$url = $_POST["form_url"];

foreach ($_POST as $a){
    if (!str_startswith($a, "form_")){
        $b = $_POST[$a];
        $message .= "$a = $b\n";
    }
}

if (isset($_POST["form_subject"])){
    $subject = $_POST["form_subject"];
} else {
    $subject = _("Message from PronaturaTours");
}

mail($mail, _("Message from PronaturaTours"), $message, $from_mail);
echo "succes?";

echo $message;
$link = rtrim(construct_link(array("page", "action")), "&");
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=$url\">";
die();
*/
?>
